<template>
  <el-container>
    <el-header><div id="logo"><img src="../../../assets/logo.png"/></div></el-header>
    <el-main>
      <label>用户名</label>
      <el-input ref="username_input" v-model="username"></el-input>
      <label>密码</label>
      <el-input v-model="password" ref="passwd_input" type="password" auto-complete="off"></el-input>
      <label>确认密码</label>
      <el-input v-model="confirm_password" ref="confirm_input" type="password" auto-complete="off"></el-input>
      <label>验证码</label>
      <img ref="vcode_img" :src="captcha" @click="doRefresh"/>
      <el-input  ref="vcode_input" v-model="vcode"></el-input>
      <el-button ref="register_btn" @click="doRegister" type="primary">提交注册</el-button>
      <el-button ref="return_btn" @click="doReturn">返回</el-button>
    </el-main>
  </el-container>
</template>

<script>
    var baseUrl = "http://129.1.9.46:8726"
    var authUrl = baseUrl + "/security"
    export default {
        name: "Register",
        data: function () {
          return {
            username: 'jimmymo',
            password: 'root1234',
            confirm_password: '',
            vcode: '',
            captcha: baseUrl+'/vcodes?scenario=register'
          }
      },
      methods: {
        doRegister(event) {
          if (event){
            this.$http.get(authUrl+"/salts?username="+this.username).then(function(res){
              var salt = res.bodyText
              // alert(salt)
              var passport = hex_sha1(this.password+salt)
              alert(passport)
              this.$http.post(authUrl+"/accounts?username="+this.username+"&passport="+passport+"&vcode="+this.vcode).then(function(res){
                this.$message({message: '注册成功',type: 'success'});
                var accessToken = res.body.accesstoken
                // var permissions = res.body.permissions
                // var roles = res.body.roles
                window.sessionStorage.setItem('username', this.username)
                window.sessionStorage.setItem('passport', passport)
                window.sessionStorage.setItem('accessToken', accessToken)
                this.$router.push("dashboard")
              }, function(err){
                this.isEdit = true
                this.$message({message: '注册失败',type: 'error'});
                console.log(err)
              })

            }, function(err){
              console.log(err)
            })
          }
        },
        doRefresh(event) {
          this.captcha = baseUrl + '/vcodes?scenario=register&d='+Math.random();
        },
        doReturn(event) {
          if (event) {
            this.$router.push("login")
          }
        }
      }
    }
</script>

<style scoped>

</style>
